package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.ProductCombination;
import org.dromara.shopping.service.IProductCombinationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 拼团产品
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/product/combination")
public class ProductCombinationController extends BaseController {

    private final IProductCombinationService productCombinationService;

    /**
     * 查询拼团产品列表
     */
    @SaCheckPermission("shopping:combination:list")
    @GetMapping("/list")
    public TableDataInfo<ProductCombination> list(ProductCombination productCombination, PageQuery pageQuery) {
        QueryWrapper<ProductCombination> qw = new QueryWrapper<>();

        TableDataInfo<ProductCombination> td = PageUtils.buildDataInfo(productCombinationService.page(PageUtils.buildPage(),qw));
        List<ProductCombination> list = td.getRows();
        return td;
    }

    /**
     * 导出拼团产品列表
     */
    @SaCheckPermission("shopping:combination:export")
    @Log(title = "拼团产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductCombination productCombination, HttpServletResponse response) {
        List<ProductCombination> list = productCombinationService.list();
        ExcelUtil.exportExcel(list, "拼团产品", ProductCombination.class, response);
    }

    /**
     * 获取拼团产品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shopping:combination:query")
    @GetMapping("/{id}")
    public R<ProductCombination> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(productCombinationService.getById(id));
    }

    /**
     * 新增拼团产品
     */
    @SaCheckPermission("shopping:combination:add")
    @Log(title = "拼团产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductCombination productCombination) {
        return toAjax(productCombinationService.save(productCombination));
    }

    /**
     * 修改拼团产品
     */
    @SaCheckPermission("shopping:combination:edit")
    @Log(title = "拼团产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCombination productCombination) {
        return toAjax(productCombinationService.updateById(productCombination));
    }

    /**
     * 删除拼团产品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:combination:remove")
    @Log(title = "拼团产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productCombinationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
